//引入样式
require('../../assets/css/normalize.css')
require('../../assets/css/reset.css')
require('../../assets/fonts/iconfont.css');
require('./login.less')

//引入模块
const dom = require('../../utils/dom.js')
const axios = require('axios')
const CaptchaMini = require('captcha-mini')

//文档加载完成时执行
document.addEventListener('DOMContentLoaded', function () {
    //获取错误提示文字
    let error = dom.get(".log-prompt-fotter");
    dom.get('.login').addEventListener('click', function () {
        let user = dom.get('.tel').value;
        let pwd = dom.get('.pwd').value;

        //账号格式验证
        const reg = /^1[3456789]\d{9}$/i
        if (reg.test(user)) {
            error.textContent = ``
        } else {
            error.textContent = `账号格式错误(请输入11位的手机号)`
            return;
        }

        if (pwd.length >= 6 && pwd.length <= 10) {
            error.textContent = ``
        } else {
            error.textContent = '请输入密码(6-10位)';
            return;
        }

        //验证账号是否存在
        axios.post('http://fitness.h5.itsource.cn:3701/api/user/isExist', {
            account: user,
        }).then(res => {
            if (res.data.errno === 0) {
                error.textContent = '用户不存在(请先注册)'
            }
        })

        // //账号存在,进行登录
        axios.post('http://fitness.h5.itsource.cn:3701/api/user/login', {
            account: user,
            password: pwd,
        }).then(res => {
            if (res.data.errno === 0) {
                location = './index.html'
                localStorage.setItem('token', res.data.data.token) 

            } else {
                error.textContent = '登录失败(密码错误)'
                return;
            }
        })
    })


})
